package com.unittec.drainage.sdk.root.internal.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletRequest;

import com.unittec.drainage.sdk.root.domain.GatewayRequest;

public class GatewayRequestParser {

    @SuppressWarnings("unchecked")
    public static GatewayRequest getData(HttpServletRequest request) {
        GatewayRequest gatewayData = new GatewayRequest();
        gatewayData.setRequestId(SdkUtils.getResultId());
        String appId = request.getParameter("app_id");
        String appKey = request.getParameter("app_key");
        String sign = request.getParameter("sign");
        String token = request.getParameter("token");
        String method = request.getParameter("method");
        String content = request.getParameter("content");
        String setting = request.getParameter("setting");
        String timestamp = request.getParameter("timestamp");
        String appSettingId = request.getParameter("app_setting_id");
        String debug = request.getParameter("debug");
        if (appId != null) {
            gatewayData.setAppId(appId);
        }

        if (appKey != null) {
            gatewayData.setAppKey(appKey);
        }

        if (sign != null) {
            gatewayData.setSign(sign);
        }

        if (token != null) {
            gatewayData.setAppKey(token);
        }

        if (method != null) {
            gatewayData.setMethod(method);
        }

        if (content != null) {
            gatewayData.setContent(content);
        }

        if (setting != null) {
            gatewayData.setTimestamp(Long.parseLong(timestamp));
        }

        if (appSettingId != null) {
            gatewayData.setAppSettingId(appSettingId);
        }

        if (debug != null) {
            gatewayData.setDebug(Boolean.parseBoolean(debug));
        }

        return gatewayData;
    }

    public static GatewayRequest postData(HttpServletRequest request) {
        try {
            String requestData = GatewayRequestParser._postData(request);
            GatewayRequest gatewayData = SdkUtils.fromJson(requestData, GatewayRequest.class);

            return gatewayData;
        } catch (Exception var4) {
            throw new RuntimeException("请求基础参数错误", var4);
        }
    }

    private static String _postData(HttpServletRequest request) {
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            String line = null;

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception var13) {
        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException var12) {
                }
            }

        }

        return buffer.toString();
    }
}
